package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import com.galgram.project.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxTeacher;
import com.galgram.project.service.IFxTeacherService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 教师Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/teacher")
public class FxTeacherController extends BaseController {
    @Autowired
    private IFxTeacherService fxTeacherService;

    /**
     * 查询教师列表
     */
    @GetMapping("/getList")
    public TableDataInfo getTeacherList(Teacher teacher) {
        startPage();
        List<Teacher> list = fxTeacherService.getTeacherList(teacher);
        return getDataTable(list);
    }

    /**
     * 查询教师列表
     */
    @PreAuthorize("@ss.hasPermi('project:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxTeacher fxTeacher) {
        startPage();
        List<FxTeacher> list = fxTeacherService.selectFxTeacherList(fxTeacher);
        return getDataTable(list);
    }

    /**
     * 获取教师详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:teacher:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxTeacherService.getById(id));
    }

    /**
     * 新增教师
     */
    @PreAuthorize("@ss.hasPermi('project:teacher:add')")
    @Log(title = "教师", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxTeacher fxTeacher) {
        fxTeacherService.save(fxTeacher);
        return success("添加成功");
    }

    /**
     * 修改教师
     */
    @PreAuthorize("@ss.hasPermi('project:teacher:edit')")
    @Log(title = "教师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxTeacher fxTeacher) {
        fxTeacherService.updateById(fxTeacher);
        return success("修改成功");
    }

    /**
     * 删除教师
     */
    @PreAuthorize("@ss.hasPermi('project:teacher:remove')")
    @Log(title = "教师", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxTeacherService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出教师列表
     */
    @PreAuthorize("@ss.hasPermi('project:teacher:export')")
    @Log(title = "教师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxTeacher fxTeacher) {
        List<FxTeacher> list = fxTeacherService.selectFxTeacherList(fxTeacher);
        ExcelUtil<FxTeacher> util = new ExcelUtil<FxTeacher>(FxTeacher.class);
        util.exportExcel(response, list, "教师数据");
    }

    /**
     * 导入教师列表
     */
    @PreAuthorize("@ss.hasPermi('project:teacher:import')")
    @Log(title = "教师", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxTeacher> util = new ExcelUtil<FxTeacher>(FxTeacher.class);
        List<FxTeacher> userList = util.importExcel(file.getInputStream());
        fxTeacherService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:teacher:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxTeacher> list = new ArrayList<FxTeacher>();
        ExcelUtil<FxTeacher> util = new ExcelUtil<FxTeacher>(FxTeacher.class);
        util.exportExcel(response, list, "教师数据");
    }
}
